
package com.xiaoma.myaudience.util.activity;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;

import com.xiaoma.myaudience.biz.activity.HomeActivity;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask.DataAsyncCallback;
import com.xiaoma.myaudience.util.view.menu.MenuBuilder;
import com.xiaoma.myaudience.util.view.menu.NeteaseMenu;

/**
 * TabActivity基础类.<br/>
 * 
 * @author wjying
 */
public class BaseTabActivity extends TabActivity implements DataAsyncCallback, OnTabChangeListener {

    private NeteaseMenu mMenu;

    private String mCurrentTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 禁用系统标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);

        getTabHost().setOnTabChangedListener(this);

        // 创建菜单
        MenuBuilder menu = new MenuBuilder(this);
        if (onCreateOptionsNeteaseMenu(menu)) {
            mMenu = new NeteaseMenu(this, menu);
        }
    }

    /**
     * Activity全屏.
     * 
     * @param b
     */
    public void makeFullScreen(boolean b) {
        if (b) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Activity child = getCurrentActivity();
        if (child != null && child instanceof BaseActivity
                && ((BaseActivity) child).willDispatchKeyEvent(event)) {
            return true;
        } else if (willDispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

    /**
     * 判断activity会不会执行此次key事件
     * 
     * @param event
     * @return
     */
    public boolean willDispatchKeyEvent(KeyEvent event) {
        return isNeteaseMenu() && mMenu.dispatchKeyEvent(event);
    }

    /**
     * 是否有菜单
     * 
     * @return
     */
    public boolean isNeteaseMenu() {
        return mMenu != null;
    }

    /**
     * 覆盖此方法使用自定义菜单.
     * 
     * @param menu
     * @return
     */
    public boolean onCreateOptionsNeteaseMenu(Menu menu) {
        return false;
    }

    @Override
    public void onCancelled(int what) {
    }

    @Override
    public void onPostExecute(int what, Object o) {
    }

    @Override
    public void onPreExecute(int what) {
    }

    @Override
    public void onProgressUpdate(int what, Object o) {
    }

    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void Tips(String message, int duration) {
        Toast.makeText(this, message, duration).show();
    }

    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void Tips(String message) {
        Tips(message, Toast.LENGTH_SHORT);
    }

    public void Tips(int resid) {
        Tips(resid, Toast.LENGTH_SHORT);
    }

    public void Tips(int resid, int duration) {
        Toast.makeText(this, resid, duration).show();
    }

    /**
     * 子类可以覆盖此方法,不用再实现TabHost.OnTabChangeListener<br/>
     */
    @Override
    public void onTabChanged(String tabId) {
        if (!TextUtils.isEmpty(mCurrentTab)) {
            Activity activity = getLocalActivityManager().getActivity(mCurrentTab);
            if (activity instanceof OnTabShowOrHideListener) {
                OnTabShowOrHideListener onTabShowOrHideListener = (OnTabShowOrHideListener) activity;
                onTabShowOrHideListener.onShowOrHide(false);
            }
        }
        
        Activity activity = getCurrentActivity();
        if (activity instanceof OnTabShowOrHideListener) {
            OnTabShowOrHideListener onTabShowOrHideListener = (OnTabShowOrHideListener) activity;
            onTabShowOrHideListener.onShowOrHide(true);
        }

        mCurrentTab = tabId;
    }

    /**
     * 当子TabChanged时调用,每个子activity都应该继承本接口<br/>
     * 
     * @author wjying
     */
    public interface OnTabShowOrHideListener {
        /**
         * 当子TabChanged时调用
         * 
         * @param show
         */
        public void onShowOrHide(boolean show);
    }
}
